package com.myth;

public class XiShu {
    public static void main(String[] arys){
        //创建棋盘数组
        int[][] arrays1 = new int[11][11];
        arrays1[1][2] = 1;
        arrays1[2][3] = 2;

        System.out.println("输出原始数组");

        //用增强for循环输出数组
        for (int[] ints:arrays1){
            for(int intss:ints){
                System.out.print(intss+"\t");
            }
            System.out.println();
        }

        //查询出原始数组有多少个有效值
        int sum = 0;
        for(int i=0;i<arrays1.length;i++ ){
            for (int j=0;j<arrays1[i].length;j++){
                if(arrays1[i][j] != 0){
                    sum++;
                }
            }
        }

        //创建稀疏数组
        int[][] arrays2 = new int[sum+1][3];
        arrays2[0][0] = arrays1.length;
        arrays2[0][1] = arrays1[0].length;
        arrays2[0][2] = sum;

        //遍历二维数组，将有效值存放在稀疏数组中
        int cnt = 0;
        for(int i=0;i<arrays1.length;i++){
            for (int j = 0; j <arrays1[i].length ; j++) {
                if (arrays1[i][j] != 0) {
                    cnt++;
                    arrays2[cnt][0] = i + 1;
                    arrays2[cnt][1] = j + 1;
                    arrays2[cnt][2] = arrays1[i][j];
                }
            }
        }

        //打印稀疏数组
        System.out.println("打印稀疏数组");
        for (int i = 0; i < arrays2.length; i++) {
            for (int j = 0; j < arrays2[i].length; j++) {
                System.out.print(arrays2[i][j]+"\t");
            }
            System.out.println();
        }

        //还原数组
        int su = 0;
        int [][] arrays3 = new int[arrays2[0][0]][arrays2[0][1]];
        for (int i = 0; i < arrays2[0][0] ; i++) {
            for (int j = 0; j < arrays2[0][1] ; j++) {
                if (i+1 == arrays2[su+1][0] && j+1 == arrays2[su][1]) {
                    arrays3[i][j] = arrays2[su+1][2];
                    su++;
                }
                else {
                    arrays3[i][j] = 0;
                }
            }
        }

        //打印还原数组
        System.out.println("打印还原数组");
        for (int i = 0; i < arrays3.length; i++) {
            for (int j = 0; j < arrays3[i].length; j++) {
                System.out.print(arrays3[i][j]+"\t");
            }
            System.out.println();
        }
    }
}
